clear all

********************************************************************************
********************************************************************************
**** When Can We Trust Regression Discontinuity Design Estimates from Close ****
*************** Elections? Evidence from Experimental Benchmarks ***************
********************************************************************************
********************************************************************************

* This file produces Figure 1 in the main text of De Magalhaes et al. (Political
* Analysis).

cd "$dir"
use "$dir/data/personal_incumbency_advantage.dta", clear

la var elected "Lottery"

********************************************************************************

* RDD PLOTS

* Dependent variable = Elected t + 1 
* Colombia

cd "$dir/temp/"	

rdbwselect electednextelection rv if rv!=0 & country=="Colombia", all vce(cluster constituency) kernel(uniform) p(2)
local h_CER2=e(h_cerrd)
rdplot electednextelection rv if abs(rv)<=`h_CER2' & rv!=0 & country=="Colombia", p(2) genvars binselect(es)
bysort rdplot_mean_y rdplot_mean_x: gen n = _n
replace rdplot_mean_y = . if n != 1
replace rdplot_mean_x = . if n != 1
rdbwselect electednextelection rv if rv!=0 & country=="Colombia", all vce(cluster constituency) kernel(uniform)
local h_MSE1=e(h_mserd)
local h_CER1=e(h_cerrd)
twoway (scatter rdplot_mean_y rdplot_mean_x if abs(rv)<=`h_CER2' & rv!=0 & country=="Colombia", plotregion(lcolor(black)) ylabel(, format(%3.2f) angle(0)) xlabel(#5, format(%3.2f)) msize(small) mcolor(gs6) xline(0, lcolor(gs12)) xtitle("Vote margin") ytitle("Elected t+1") legend(off) graphregion(color(white))) ///
	(lfit electednextelection rv if rv<0 & abs(rv)<=`h_CER1' & country=="Colombia", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit electednextelection rv if rv<0 & abs(rv)<=`h_MSE1' & country=="Colombia", lcolor(red) lwidth(medthick)) ///
	(qfit electednextelection rv if rv<0 & abs(rv)<=`h_CER2' & country=="Colombia", lcolor(blue) lwidth(thin) lpattern(--)) ///
	(lfit electednextelection rv if rv>0 & abs(rv)<=`h_CER1' & country=="Colombia", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit electednextelection rv if rv>0 & abs(rv)<=`h_MSE1' & country=="Colombia", lcolor(red) lwidth(medthick)) ///
	(qfit electednextelection rv if rv>0 & abs(rv)<=`h_CER2' & country=="Colombia", lcolor(blue) lwidth(thin) lpattern(--))
graph save "colombia_elected_rdplot.gph", replace
drop rdplot* n

* Finland

rdbwselect electednextelection rv if rv!=0 & country=="Finland", all vce(cluster constituency) kernel(uniform) p(2)
local h_CER2=e(h_cerrd)
rdplot electednextelection rv if abs(rv)<=`h_CER2' & rv!=0 & country=="Finland", p(2) genvars binselect(es)
bysort rdplot_mean_y rdplot_mean_x: gen n = _n
replace rdplot_mean_y = . if n != 1
replace rdplot_mean_x = . if n != 1
rdbwselect electednextelection rv if rv!=0 & country=="Finland", all vce(cluster constituency) kernel(uniform)
local h_MSE1=e(h_mserd)
local h_CER1=e(h_cerrd)
twoway (scatter rdplot_mean_y rdplot_mean_x if abs(rv)<=`h_CER2' & rv!=0 & country=="Finland", plotregion(lcolor(black)) ylabel(, format(%3.2f) angle(0)) xlabel(#5, format(%3.2f)) msize(small) mcolor(gs6) xline(0, lcolor(gs12)) xtitle("Vote margin") ytitle("Elected t+1") legend(off) graphregion(color(white))) ///
	(lfit electednextelection rv if rv<0 & abs(rv)<=`h_CER1' & country=="Finland", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit electednextelection rv if rv<0 & abs(rv)<=`h_MSE1' & country=="Finland", lcolor(red) lwidth(medthick)) ///
	(qfit electednextelection rv if rv<0 & abs(rv)<=`h_CER2' & country=="Finland", lcolor(blue) lwidth(thin) lpattern(--)) ///
	(lfit electednextelection rv if rv>0 & abs(rv)<=`h_CER1' & country=="Finland", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit electednextelection rv if rv>0 & abs(rv)<=`h_MSE1' & country=="Finland", lcolor(red) lwidth(medthick)) ///
	(qfit electednextelection rv if rv>0 & abs(rv)<=`h_CER2' & country=="Finland", lcolor(blue) lwidth(thin) lpattern(--))
graph save "finland_elected_rdplot.gph", replace
drop rdplot* n

* Dependent variable = Running t + 1
* Colombia

rdbwselect runningnextelection rv if rv!=0 & country=="Colombia", all vce(cluster constituency) kernel(uniform) p(2)
local h_CER2=e(h_cerrd)
rdplot runningnextelection rv if abs(rv)<=`h_CER2' & rv!=0 & country=="Colombia", p(2) genvars binselect(es)
bysort rdplot_mean_y rdplot_mean_x: gen n = _n
replace rdplot_mean_y = . if n != 1
replace rdplot_mean_x = . if n != 1
rdbwselect runningnextelection rv if rv!=0 & country=="Colombia", all vce(cluster constituency) kernel(uniform)
local h_MSE1=e(h_mserd)
local h_CER1=e(h_cerrd)
twoway (scatter rdplot_mean_y rdplot_mean_x if abs(rv)<=`h_CER2' & rv!=0 & country=="Colombia", plotregion(lcolor(black)) ylabel(, format(%3.2f) angle(0)) xlabel(#5, format(%3.2f)) msize(small) mcolor(gs6) xline(0, lcolor(gs12)) xtitle("Vote margin") ytitle("Elected t+1") legend(off) graphregion(color(white))) ///
	(lfit runningnextelection rv if rv<0 & abs(rv)<=`h_CER1' & country=="Colombia", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit runningnextelection rv if rv<0 & abs(rv)<=`h_MSE1' & country=="Colombia", lcolor(red) lwidth(medthick)) ///
	(qfit runningnextelection rv if rv<0 & abs(rv)<=`h_CER2' & country=="Colombia", lcolor(blue) lwidth(thin) lpattern(--)) ///
	(lfit runningnextelection rv if rv>0 & abs(rv)<=`h_CER1' & country=="Colombia", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit runningnextelection rv if rv>0 & abs(rv)<=`h_MSE1' & country=="Colombia", lcolor(red) lwidth(medthick)) ///
	(qfit runningnextelection rv if rv>0 & abs(rv)<=`h_CER2' & country=="Colombia", lcolor(blue) lwidth(thin) lpattern(--))
graph save "colombia_rerun_rdplot.gph", replace
drop rdplot* n

* Finland

rdbwselect runningnextelection rv if rv!=0 & country=="Finland", all vce(cluster constituency) kernel(uniform) p(2)
local h_CER2=e(h_cerrd)
rdplot runningnextelection rv if abs(rv)<=`h_CER2' & rv!=0 & country=="Finland", p(2) genvars binselect(es)
bysort rdplot_mean_y rdplot_mean_x: gen n = _n
replace rdplot_mean_y = . if n != 1
replace rdplot_mean_x = . if n != 1
rdbwselect runningnextelection rv if rv!=0 & country=="Finland", all vce(cluster constituency) kernel(uniform)
local h_MSE1=e(h_mserd)
local h_CER1=e(h_cerrd)
twoway (scatter rdplot_mean_y rdplot_mean_x if abs(rv)<=`h_CER2' & rv!=0 & country=="Finland", plotregion(lcolor(black)) ylabel(, format(%3.2f) angle(0)) xlabel(#5, format(%3.2f)) msize(small) mcolor(gs6) xline(0, lcolor(gs12)) xtitle("Vote margin") ytitle("Elected t+1") legend(off) graphregion(color(white))) ///
	(lfit runningnextelection rv if rv<0 & abs(rv)<=`h_CER1' & country=="Finland", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit runningnextelection rv if rv<0 & abs(rv)<=`h_MSE1' & country=="Finland", lcolor(red) lwidth(medthick)) ///
	(qfit runningnextelection rv if rv<0 & abs(rv)<=`h_CER2' & country=="Finland", lcolor(blue) lwidth(thin) lpattern(--)) ///
	(lfit runningnextelection rv if rv>0 & abs(rv)<=`h_CER1' & country=="Finland", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit runningnextelection rv if rv>0 & abs(rv)<=`h_MSE1' & country=="Finland", lcolor(red) lwidth(medthick)) ///
	(qfit runningnextelection rv if rv>0 & abs(rv)<=`h_CER2' & country=="Finland", lcolor(blue) lwidth(thin) lpattern(--))
graph save "finland_rerun_rdplot.gph", replace
drop rdplot* n

********************************************************************************

* ROBUSTNESS TO ALTERNATIVE BANDWIDTHS
* Dependent variable = Elected t + 1
* Colombia

cd "$dir/data"
use "personal_incumbency_advantage.dta", clear

reg electednextelection elected if rv==0 & country=="Colombia", cluster(constituency)
parmest, saving("$dir/temp/colombia_lottery.dta", replace)
use "$dir/temp/colombia_lottery.dta", clear
gen bandwidth=0
keep if parm=="elected"
save "$dir/temp/colombia_lottery.dta", replace

quietly {
	use "$dir/data/personal_incumbency_advantage.dta", clear
	
	rdbwselect electednextelection rv if country=="Colombia" & rv!=0, all vce(cluster constituency) kernel(uniform)
	local h_MSE=e(h_mserd)
	local h_CER=e(h_cerrd)
	rdbwselect electednextelection rv if country=="Colombia" & rv!=0, all vce(cluster constituency) kernel(uniform) p(2)
	local h_MSE2=e(h_mserd)
	local h_CER2=e(h_cerrd)
	
	cd "$dir/temp"
	
	forvalues c = 10(1)400 {
		local h = `c'/100
		rdrobust electednextelection rv if country=="Colombia" & rv!=0, rho(1) all vce(cluster constituency) kernel(uniform) h(`h')
		parmest, saving(locallinear_colombia_rdrobust`c'.dta, replace)
		}

	forvalues i = 10(1)400 {
		use locallinear_colombia_rdrobust`i'.dta, clear
		gen bandwidth = `i'/100
		save, replace
		}

	use locallinear_colombia_rdrobust10.dta, clear

	forvalues i = 11(1)400 {
		append using locallinear_colombia_rdrobust`i'.dta
		}
	append using "colombia_lottery.dta"

	sum bandwidth if parm=="Conventional"
	local jump = 0.5
	local xmin = 0
	local xmax = r(max)

	twoway (scatter estimate bandwidth if parm=="Conventional", connect(direct) lwidth(medthick) lcolor(red) ///
		msymbol(none)) (rcap min95 max95 bandwidth if bandwidth==0, lcolor(black)) ///
		(scatter estimate bandwidth if bandwidth==0, mcolor(black) fcolor(white)) ///
		(rarea min95 max95 bandwidth if parm=="Robust", fcolor(blue%30) lwidth(none)) ///
		(scatter estimate bandwidth if parm=="Robust", ///
		connect(direct) lwidth(medium) lcolor(blue) lwidth(thin) lpattern(--) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Conventional", fcolor(red%30) lwidth(none) lpattern(solid) ///
		plotregion(lcolor(black)) xline(`h_MSE', lcolor(red) lpattern(solid)) ///
		xline(`h_CER', lpattern(dash) lcolor(red))  ///
		xline(`h_MSE2', lcolor(blue) lwidth(thin) lpattern(solid)) ///
		xline(`h_CER2', lpattern(dash) lcolor(blue) lwidth(thin)) ///
		xlabel(`xmin'(`jump')`xmax', format(%3.2f)) ylabel(, format(%3.2f) angle(0)) ///
		yline(0, lcolor(black) lwidth(vthin)) legend(off) xtitle("Bandwidth") ///
		ytitle(Estimated effect) graphregion(color(white))) 
		
	graph save locallinear_colombia_standard1.gph, replace

	sum bandwidth if parm=="Robust"
	local jump = 0.5
	local xmin = 0.5
	local xmax = r(max)	
	}

* Finland	
	
cd "$dir/data"
use "personal_incumbency_advantage.dta", clear

reg electednextelection elected if rv==0 & country=="Finland", cluster(constituency)
parmest, saving("$dir/temp/finland_lottery.dta", replace)
use "$dir/temp/finland_lottery.dta", clear
gen bandwidth=0
keep if parm=="elected"
save "$dir/temp/finland_lottery.dta", replace

quietly {
	use "$dir/data/personal_incumbency_advantage.dta", clear
	
	rdbwselect electednextelection rv if country=="Finland" & rv!=0, all vce(cluster constituency) kernel(uniform)
	local h_MSE=e(h_mserd)
	local h_CER=e(h_cerrd)
	rdbwselect electednextelection rv if country=="Finland" & rv!=0, all vce(cluster constituency) kernel(uniform) p(2)
	local h_MSE2=e(h_mserd)
	local h_CER2=e(h_cerrd)
	
	cd "$dir/temp"
	
	forvalues c = 10(1)400 {
		local h = `c'/100
		rdrobust electednextelection rv if country=="Finland" & rv!=0, rho(1) all vce(cluster constituency) kernel(uniform) h(`h')
		parmest, saving(locallinear_finland_rdrobust`c'.dta, replace)
		}

	forvalues i = 10(1)400 {
		use locallinear_finland_rdrobust`i'.dta, clear
		gen bandwidth = `i'/100
		save, replace
		}

	use locallinear_finland_rdrobust10.dta, clear

	forvalues i = 11(1)400 {
		append using locallinear_finland_rdrobust`i'.dta
		}
	append using "finland_lottery.dta"

	sum bandwidth if parm=="Conventional"
	local jump = 0.5
	local xmin = 0
	local xmax = r(max)

	twoway (scatter estimate bandwidth if parm=="Conventional", connect(direct) lwidth(medthick) lcolor(red) ///
		msymbol(none)) (rcap min95 max95 bandwidth if bandwidth==0, lcolor(black)) ///
		(scatter estimate bandwidth if bandwidth==0, mcolor(black) fcolor(white)) ///
		(rarea min95 max95 bandwidth if parm=="Robust", fcolor(blue%30) lwidth(none)) ///
		(scatter estimate bandwidth if parm=="Robust", ///
		connect(direct) lwidth(medium) lcolor(blue) lwidth(thin) lpattern(--) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Conventional", fcolor(red%30) lwidth(none) lpattern(solid) ///
		plotregion(lcolor(black)) xline(`h_MSE', lcolor(red) lpattern(solid)) ///
		xline(`h_CER', lpattern(dash) lcolor(red))  ///
		xline(`h_MSE2', lcolor(blue) lwidth(thin) lpattern(solid)) ///
		xline(`h_CER2', lpattern(dash) lcolor(blue) lwidth(thin)) ///
		xlabel(`xmin'(`jump')`xmax', format(%3.2f)) ylabel(, format(%3.2f) angle(0)) ///
		yline(0, lcolor(black) lwidth(vthin)) legend(off) xtitle("Bandwidth") ///
		ytitle(Estimated effect) graphregion(color(white))) 

		graph save locallinear_finland_standard1.gph, replace

	}

* Dependent variable = Running t + 1
* Colombia

cd "$dir/data"
use "personal_incumbency_advantage.dta", clear

reg runningnextelection elected if rv==0 & country=="Colombia", cluster(constituency)
parmest, saving("$dir/temp/colombia_lottery.dta", replace)
use "$dir/temp/colombia_lottery.dta", clear
gen bandwidth=0
keep if parm=="elected"
save "$dir/temp/colombia_lottery.dta", replace

quietly {
	use "$dir/data/personal_incumbency_advantage.dta", clear
	
	rdbwselect runningnextelection rv if country=="Colombia" & rv!=0, all vce(cluster constituency) kernel(uniform)
	local h_MSE=e(h_mserd)
	local h_CER=e(h_cerrd)
	rdbwselect runningnextelection rv if country=="Colombia" & rv!=0, all vce(cluster constituency) kernel(uniform) p(2)
	local h_MSE2=e(h_mserd)
	local h_CER2=e(h_cerrd)
	
	cd "$dir/temp"
	
	forvalues c = 10(1)400 {
		local h = `c'/100
		rdrobust runningnextelection rv if country=="Colombia" & rv!=0, rho(1) all vce(cluster constituency) kernel(uniform) h(`h')
		parmest, saving(locallinear_colombia_rdrobust`c'.dta, replace)
		}

	forvalues i = 10(1)400 {
		use locallinear_colombia_rdrobust`i'.dta, clear
		gen bandwidth = `i'/100
		save, replace
		}

	use locallinear_colombia_rdrobust10.dta, clear

	forvalues i = 11(1)400 {
		append using locallinear_colombia_rdrobust`i'.dta
		}
	append using "colombia_lottery.dta"

	sum bandwidth if parm=="Conventional"
	local jump = 0.5
	local xmin = 0
	local xmax = r(max)

	twoway (scatter estimate bandwidth if parm=="Conventional", connect(direct) lwidth(medthick) lcolor(red) ///
		msymbol(none)) (rcap min95 max95 bandwidth if bandwidth==0, lcolor(black)) ///
		(scatter estimate bandwidth if bandwidth==0, mcolor(black) fcolor(white)) ///
		(rarea min95 max95 bandwidth if parm=="Robust", fcolor(blue%30) lwidth(none)) ///
		(scatter estimate bandwidth if parm=="Robust", ///
		connect(direct) lwidth(medium) lcolor(blue) lwidth(thin) lpattern(--) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Conventional", fcolor(red%30) lwidth(none) lpattern(solid) ///
		plotregion(lcolor(black)) ///
		xline(`h_CER', lpattern(dash) lcolor(red))  ///
		xlabel(`xmin'(`jump')`xmax', format(%3.2f)) ylabel(, format(%3.2f) angle(0)) ///
		yline(0, lcolor(black) lwidth(vthin)) legend(off) xtitle("Bandwidth") ///
		ytitle(Estimated effect) graphregion(color(white))) 

		graph save locallinear_colombia_rerun_standard1.gph, replace

	sum bandwidth if parm=="Robust"
	local jump = 0.5
	local xmin = 0.5
	local xmax = r(max)	

	}

* Finland	
	
cd "$dir/data"
use "personal_incumbency_advantage.dta", clear

reg runningnextelection elected if rv==0 & country=="Finland", cluster(constituency)
parmest, saving("$dir/temp/finland_lottery.dta", replace)
use "$dir/temp/finland_lottery.dta", clear
gen bandwidth=0
keep if parm=="elected"
save "$dir/temp/finland_lottery.dta", replace

quietly {
	use "$dir/data/personal_incumbency_advantage.dta", clear
	
	rdbwselect runningnextelection rv if country=="Finland" & rv!=0, all vce(cluster constituency) kernel(uniform)
	local h_MSE=e(h_mserd)
	local h_CER=e(h_cerrd)
	rdbwselect runningnextelection rv if country=="Finland" & rv!=0, all vce(cluster constituency) kernel(uniform) p(2)
	local h_MSE2=e(h_mserd)
	local h_CER2=e(h_cerrd)
	
	cd "$dir/temp"
	
	forvalues c = 10(1)400 {
		local h = `c'/100
		rdrobust runningnextelection rv if country=="Finland" & rv!=0, rho(1) all vce(cluster constituency) kernel(uniform) h(`h')
		parmest, saving(locallinear_finland_rdrobust`c'.dta, replace)
		}

	forvalues i = 10(1)400 {
		use locallinear_finland_rdrobust`i'.dta, clear
		gen bandwidth = `i'/100
		save, replace
		}

	use locallinear_finland_rdrobust10.dta, clear

	forvalues i = 11(1)400 {
		append using locallinear_finland_rdrobust`i'.dta
		}
	append using "finland_lottery.dta"

	sum bandwidth if parm=="Conventional"
	local jump = 0.5
	local xmin = 0
	local xmax = r(max)

	twoway (scatter estimate bandwidth if parm=="Conventional", connect(direct) lwidth(medthick) lcolor(red) ///
		msymbol(none)) (rcap min95 max95 bandwidth if bandwidth==0, lcolor(black)) ///
		(scatter estimate bandwidth if bandwidth==0, mcolor(black) fcolor(white)) ///
		(rarea min95 max95 bandwidth if parm=="Robust", fcolor(blue%30) lwidth(none)) ///
		(scatter estimate bandwidth if parm=="Robust", ///
		connect(direct) lwidth(medium) lcolor(blue) lwidth(thin) lpattern(--) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Conventional", fcolor(red%30) lwidth(none) lpattern(solid) ///
		plotregion(lcolor(black)) xline(`h_MSE', lcolor(red) lpattern(solid)) ///
		xline(`h_CER', lpattern(dash) lcolor(red))  ///
		xline(`h_MSE2', lcolor(blue) lwidth(thin) lpattern(solid)) ///
		xline(`h_CER2', lpattern(dash) lcolor(blue) lwidth(thin)) ///
		xlabel(`xmin'(`jump')`xmax', format(%3.2f)) ylabel(, format(%3.2f) angle(0)) ///
		yline(0, lcolor(black) lwidth(vthin)) legend(off) xtitle("Bandwidth") ///
		ytitle(Estimated effect) graphregion(color(white))) 

		graph save locallinear_finland_rerun_standard1.gph, replace

	}

********************************************************************************

* Combine the graphs

graph combine "colombia_rerun_rdplot.gph" "locallinear_colombia_rerun_standard1.gph", graphregion(color(white)) cols(2) subtitle("Colombia", position(11))
graph save "colombia_rerun.gph", replace
graph combine "finland_rerun_rdplot.gph" "locallinear_finland_rerun_standard1.gph", graphregion(color(white)) cols(2) subtitle("Finland", position(11))
graph save "finland_rerun.gph", replace

graph combine "colombia_rerun.gph" "finland_rerun.gph", graphregion(color(white)) cols(1) xsize(3) ysize(2) subtitle("Panel A: Running t+1", position(11))

graph export "$dir/figures/figure_1A.pdf", as(pdf) replace

graph combine "colombia_elected_rdplot.gph" "locallinear_colombia_standard1.gph", graphregion(color(white)) cols(2) subtitle("Colombia", position(11))
graph save "colombia_elected.gph", replace
graph combine "finland_elected_rdplot.gph" "locallinear_finland_standard1.gph", graphregion(color(white)) cols(2) subtitle("Finland", position(11))
graph save "finland_elected.gph", replace

graph combine "colombia_elected.gph" "finland_elected.gph", graphregion(color(white)) cols(1) xsize(3) ysize(2) subtitle("Panel B: Elected t+1", position(11))
graph export "$dir/figures/figure_1B.pdf", as(pdf) replace
